package com.rabbitmq.config.priority;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王霄
 * @date 2022/1/8 20:51
 */
@Configuration
public class PriorityConfig {

    public static final String PRIORITY_EXCHANGE_NAME = "priority.exchange";
    public static final String PRIORITY_QUEUE = "priority.queue";
    public static final String PRIORITY_ROUTING_KEY = "priority.routing.key";


    @Bean(PRIORITY_EXCHANGE_NAME)
    public DirectExchange priorityExchange() {
        return new DirectExchange(PRIORITY_EXCHANGE_NAME);
    }

    @Bean(PRIORITY_QUEUE)
    public Queue priorityQueue() {
        Map<String, Object> arguments = new HashMap<>(2);
        arguments.put("x-max-priority", 3);
        return new Queue(PRIORITY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding bindingPriorityExchange() {
        return BindingBuilder
                .bind(priorityQueue())
                .to(priorityExchange())
                .with(PRIORITY_ROUTING_KEY);
    }

}
